<?php
	class HTML {
		private static $inputID = 0;
		
		function dump($obj){
			print '<pre>';
			print_r($obj);
			print '</pre>';
			print "\n";
		}
		
		function makeID(){
			return 'input'.HTML::$inputID++;
		}
		
		function url($resource='', $action='', $id=''){
			if ($id !== '' and $action != '' and $resource != '')
				return sprintf("%s/%s/%s/%s", Config::$basepath, $resource, $action, $id);
			if ($action != '' and $resource != '')
				return sprintf("%s/%s/%s", Config::$basepath, $resource, $action);
			if ($resource != '')
				return sprintf("%s/%s", Config::$basepath, $resource);
			return sprintf("%s/", Config::$basepath);
		}
		
		function a($text, $resource='', $action='', $id=''){
			return sprintf('<a href="%s" title="%s">%2$s</a>',HTML::url($resource,$action,$id),$text);
		}
		
		function form($name, $resource='', $action='', $id=''){
			return sprintf('<form name="%s" action="%s" method="post">', $name, HTML::url($resource,$action,$id));
		}
		
		function input($name, $attrs=array()){
			extract($attrs);
			// Setup default values
			$attrs['id']	= isset($id) ? $id : HTML::makeID($name);
			$attrs['type']	= isset($type) ? $type : 'text';
			$attrs['value']	= isset($value) ? $value : '';
			$attrs['name']	= $name;
			$attrs['class']	= isset($class) ? $class.' '.$attrs['type'] : $attrs['type'];
			
			$htmlAttrs = array();
			foreach ( $attrs as $attr => $value ){
				$htmlAttrs[] = sprintf('%s="%s"', $attr, $value);
			}
			$attrList = join(' ', $htmlAttrs);
			
			return "<input $attrList>";
		}
		
		function checkbox($name, $params=array()){
			$params['type'] = 'checkbox';
			return HTML::input($name, $params);
		}	
		
		function hiddenInput($name, $value, $attrs=array()){
			$attrs['value']	= $value;
			$attrs['type']	= 'hidden';
			return HTML::input($name, $attrs);
		}
		
		function submit($value, $attrs=array()){
			$attrs['type']	= 'submit';
			$attrs['value']	= $value;
			$attrs['class'] = isset($attrs['class']) ? $attrs['class'] .= ' button' : 'button';
			return HTML::input('submit', $attrs);
		}
		
		function labeledInput($label, $name, $attrs=array()){
			extract($attrs);
			// Setup defaults
			$labelAfter	= isset($labelAfter) ? $labelAfter : false;
			$class		= isset($class) ? $class : '';
			
			$input = HTML::input($name, $attrs);
			$format = '<label class="%s" title="%2$s">%2$s %3$s</label>';
			if ( $labelAfter ) $format = '<label class="%s" title="%2$s">%3$s %2$s</label>';
			$label = sprintf($format, $class, $label, $input);
			return $label;
		}
		
		function labeledSelect($label, $name, $members, $params=array()){
			extract($params);
			// Setup defaults
			$for		= isset($id) ? $id : HTML::makeID();
			$labelAfter	= isset($labelAfter) ? $labelAfter : false;
			$class		= isset($class) ? $class : '';
			
			$attrs['id'] = $for;
			$input = HTML::select($name, $members, $params);
			$label = sprintf('<label class="%s" for="%s" title="%s">%3$s</label>', $class, $for, $label);
			if ( $labelAfter ) return $input.$label;
			return $label.$input;
		}
		
		function select($name, $members, $params=array()){
			extract($params);
			// Setup defaults
			$selected	= isset($selected) ? $selected : '';
			$useID		= isset($useID) ? $useID : false;
			$options = '';
			$found = false;
			if ( $selected == '' ) $options .= '<option></option>';			
			foreach ($members as $id => $member){
				$attr = '';
				$value = $useID ? $id : $member;
				if ( $value == $selected ){
					$attr = 'selected="selected"';
					$found = true;	
				}
				$options .= sprintf('<option %s value="%s">%s</option>'."\n", $attr, $value, $member);
			}
			
			if ( $selected <> '' and ! $found ){
				$member = $useID ? '' : $selected;
				$value = $selected;
				$options .= sprintf('<option selected="selected" value="%s">%s</option>'."\n", $value, $member);
			}
			
			return sprintf('<select name="%s">%s</select>', $name, $options);		
		}
	}
	
	class HTTP{
		function redirect($resource='', $action='', $id=''){
			if ( Config::$debug ) return;
			//print HTML::a('Continue', $resource, $action, $id);
			//exit;
			header('Location: '.HTML::url($resource, $action, $id));
			exit;
		}
		
		function error($message){
			header('HTTP/1.0 500 Internal Server Error');
			Controller::loadView('errors/500', '500', array('message'=>$message));
			exit;
		}
		
		function mySQLError(){
			header('HTTP/1.0 500 Internal Server Error');
			Controller::loadView('errors/mysql', '500');
			exit;
		}
		
		function notFound(){
			header('HTTP/1.0 404 Not Found');
			Controller::loadView('errors/404', '404');
			exit;
		}
	}
